Type CalCoreMesh

	Field m_vectorCoreSubmesh:CalCoreSubmesh[]
	Field m_name:String
	Field m_filename:String
	
	Method New()
	
		m_vectorCoreSubmesh = New CalCoreSubmesh[0]
	
	End	Method
	
	
	Method addCoreSubmesh:Int(coreSubmesh:CalCoreSubmesh)
	
		Local submeshId:Int = m_vectorCoreSubmesh.Length
		m_vectorCoreSubmesh = m_vectorCoreSubmesh[..submeshId+1] '.push(coreSubmesh)
		m_vectorCoreSubmesh[submeshId] = coreSubmesh
		Return submeshId		
	
	End	Method
	
	Method getCoreSubmesh:CalCoreSubmesh(coreSubmeshId:Int)
	
		If coreSubmeshId < 0 Or coreSubmeshId >= m_vectorCoreSubmesh.length Then
			DebugLog "Invalid Submesh Handle"
			Return Null
		EndIf
	
		Return m_vectorCoreSubmesh[coreSubmeshId]	
	
	End	Method
	
	Method getCoreSubmeshCount:Int()
		Return m_vectorCoreSubmesh.length
	End	Method
	
	Method getVectorCoreSubmesh:CalCoreSubmesh[]()
	
		Return m_vectorCoreSubmesh
	
	End	Method

	
	Method addAsMorphTarget:Int(coreMesh:CalCoreMesh)

		Local otherVectorCoreSubmesh:CalCoreSubmesh[] = coreMesh.getVectorCoreSubmesh()
		
		If m_vectorCoreSubmesh.length <> otherVectorCoreSubmesh.length Then
			DebugLog "Morph Mesh Mismatch"
			Return -1
		EndIf
		
		If m_vectorCoreSubmesh.length = 0 Then
			DebugLog "Empty Submeshes"
			Return -1
		EndIf
	
		Local submeshId:Int = 0
		Local subMorphTargetId:Int = m_vectorCoreSubmesh[submeshId].getCoreSubMorphTargetCount()
		
		While submeshId < m_vectorCoreSubmesh.length
		

			If m_vectorCoreSubmesh[submeshId].getVertexCount() <> otherVectorCoreSubmesh[submeshId].getVertexCount() Then
				DebugLog "Vertice Mismatch"
				Return -1
			EndIf
						
			submeshId=submeshId+1
			
		Wend
	
		 
		'	Add the blend targets to each of the core sub meshes
		
		submeshId = 0
		
		While submeshId < m_vectorCoreSubmesh.length
		
			Local vertexCount:Int = otherVectorCoreSubmesh[submeshId].getVertexCount()
			Local coreSubMorphTarget:CalCoreSubMorphTarget = New CalCoreSubMorphTarget
			
			If coreSubMorphTarget.reserve(vertexCount) = False Then
				Return -1
			EndIf
			
			Local vectorVertex:CalVertex[] = otherVectorCoreSubmesh[submeshId].getVectorVertex()
			
			Local vertexId:Int = 0
			
			For Local i:Int = 0 To vertexCount-1
				Local blendVertex:CalBlendVertex = New CalBlendVertex
				
				blendVertex.position.assignVector(vectorVertex[vertexId].position)
				blendVertex.normal.assignVector(vectorVertex[vertexId].normal)
				
				If coreSubMorphTarget.setBlendVertex(i, blendVertex) = False Then
					Return -1
				EndIf
				
				vertexId=vertexId+1
			Next
	
			m_vectorCoreSubmesh[submeshId].addCoreSubMorphTarget(coreSubMorphTarget)
			submeshId=submeshId+1
		Wend	

	
		Return subMorphTargetId	
	
	End	Method
	
	
	Method scale(factor:Float)
	
		For Local submeshId:Int=0 To m_vectorCoreSubmesh.length-1
			m_vectorCoreSubmesh[submeshId].scale(factor)
		Next	
	
	End	Method
	
	Method setFilename(filename:String)
		m_filename = filename
	End	Method
	
	Method getFilename:String()
		Return m_filename
	End	Method
	
	Method setName(name:String)
		m_name = name
	End	Method
	
	Method getName:String()
		Return m_name
	End	Method

End Type
